home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 290_02 / yylex.c < prev   
Encoding:
C/C++ Source or Header  |  1990-05-12  |  4.9 KB  |  225 lines

  1. /* yylex - scanner front-end for flex */
  2.  
  3. #include "flexdef.h"
  4. #include "parse.h"
  5.  
  6. extern int flexscan( void) ;    /* from scan.c */
  7. int yylex( void) ;              /* for lint */
  8.  
  9. /*
  10.  * Copyright (c) 1987, the University of California
  11.  * 
  12.  * The United States Government has rights in this work pursuant to
  13.  * contract no. DE-AC03-76SF00098 between the United States Department of
  14.  * Energy and the University of California.
  15.  * 
  16.  * This program may be redistributed.  Enhancements and derivative works
  17.  * may be created provided the new works, if made available to the general
  18.  * public, are made available for use by anyone.
  19.  */
  20.  
  21. /* yylex - scan for a regular expression token
  22.  *
  23.  * synopsis
  24.  *
  25.  *   token = yylex();
  26.  *
  27.  *     token - return token found
  28.  */
  29.  
  30. int yylex()
  31.  
  32. {
  33.     int toktype;
  34.     static int beglin = false;
  35.  
  36.     if ( eofseen )
  37.         toktype = EOF;
  38.     else
  39.         toktype = flexscan();
  40.  
  41.     if ( toktype == EOF )
  42.     {
  43.         eofseen = 1;
  44.  
  45.         if ( sectnum == 1 )
  46.         {
  47.             synerr( "unexpected EOF" );
  48.             sectnum = 2;
  49.             toktype = SECTEND;
  50.         }
  51.  
  52.         else if ( sectnum == 2 )
  53.         {
  54.             sectnum = 3;
  55.             toktype = SECTEND;
  56.         }
  57.  
  58.         else
  59.             toktype = 0;
  60.     }
  61.  
  62.     if ( trace )
  63.     {
  64.         if ( beglin )
  65.         {
  66.             fprintf( stderr, "%d\t", accnum + 1 );
  67.             beglin = 0;
  68.         }
  69.  
  70.         switch ( toktype )
  71.         {
  72.         case '<':
  73.         case '>':
  74.         case '^':
  75.         case '$':
  76.         case '"':
  77.         case '[':
  78.         case ']':
  79.         case '{':
  80.         case '}':
  81.         case '|':
  82.         case '(':
  83.         case ')':
  84.         case '-':
  85.         case '/':
  86.         case '\\':
  87.         case '?':
  88.         case '.':
  89.         case '*':
  90.         case '+':
  91.         case ',':
  92.             (void) putc( toktype, stderr );
  93.             break;
  94.  
  95.         case '\n':
  96.             (void) putc( '\n', stderr );
  97.  
  98.             if ( sectnum == 2 )
  99.                 beglin = 1;
  100.  
  101.             break;
  102.  
  103.         case SCDECL:
  104.             fputs( "%s", stderr );
  105.             break;
  106.  
  107.         case XSCDECL:
  108.             fputs( "%x", stderr );
  109.             break;
  110.  
  111.         case WHITESPACE:
  112.             (void) putc( ' ', stderr );
  113.             break;
  114.  
  115.         case SECTEND:
  116.             fputs( "%%\n", stderr );
  117.  
  118.             /* we set beglin to be true so we'll start
  119.                  * writing out numbers as we echo rules.  flexscan() has
  120.                  * already assigned sectnum
  121.                  */
  122.  
  123.             if ( sectnum == 2 )
  124.                 beglin = 1;
  125.  
  126.             break;
  127.  
  128.         case NAME:
  129.             fprintf( stderr, "'%s'", nmstr );
  130.             break;
  131.  
  132.         case CHAR:
  133.             switch ( yylval )
  134.             {
  135.             case '<':
  136.             case '>':
  137.             case '^':
  138.             case '$':
  139.             case '"':
  140.             case '[':
  141.             case ']':
  142.             case '{':
  143.             case '}':
  144.             case '|':
  145.             case '(':
  146.             case ')':
  147.             case '-':
  148.             case '/':
  149.             case '\\':
  150.             case '?':
  151.             case '.':
  152.             case '*':
  153.             case '+':
  154.             case ',':
  155.                 fprintf( stderr, "\\%c", yylval );
  156.                 break;
  157.  
  158.             case 1:
  159.             case 2:
  160.             case 3:
  161.             case 4:
  162.             case 5:
  163.             case 6:
  164.             case 7:
  165.             case 8:
  166.             case 9:
  167.             case 10:
  168.             case 11:
  169.             case 12:
  170.             case 13:
  171.             case 14:
  172.             case 15:
  173.             case 16:
  174.             case 17:
  175.             case 18:
  176.             case 19:
  177.             case 20:
  178.             case 21:
  179.             case 22:
  180.             case 23:
  181.             case 24:
  182.             case 25:
  183.             case 26:
  184.             case 27:
  185.             case 28:
  186.             case 29:
  187.             case 30:
  188.             case 31:
  189.                 fprintf( stderr, "^%c", 'A' + yylval - 1 );
  190.                 break;
  191.  
  192.             case 127:
  193.                 (void) putc( '^', stderr );
  194.                 (void) putc( '@', stderr );
  195.                 break;
  196.  
  197.             default:
  198.                 (void) putc( yylval, stderr );
  199.                 break;
  200.             }
  201.  
  202.             break;
  203.  
  204.         case NUMBER:
  205.             fprintf( stderr, "%d", yylval );
  206.             break;
  207.  
  208.         case PREVCCL:
  209.             fprintf( stderr, "[%d]", yylval );
  210.             break;
  211.  
  212.         case 0:
  213.             fprintf( stderr, "End Marker" );
  214.             break;
  215.  
  216.         default:
  217.             fprintf( stderr, "*Something Weird* - tok: %d val: %d\n",
  218.               toktype, yylval );
  219.             break;
  220.         }
  221.     }
  222.  
  223.     return ( toktype );
  224. }
  225.